package com.prod.web.controller.shop;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ZipUtil;
import com.prod.common.config.RuoYiConfig;
import com.prod.common.constant.Constants;
import com.prod.common.utils.StringUtils;
import com.prod.common.utils.file.FileUtils;
import com.prod.system.domain.TShopImages;
import com.prod.system.service.ITShopImagesService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.prod.common.annotation.Log;
import com.prod.common.core.controller.BaseController;
import com.prod.common.core.domain.AjaxResult;
import com.prod.common.enums.BusinessType;
import com.prod.common.utils.poi.ExcelUtil;
import com.prod.common.core.page.TableDataInfo;

/**
 * 店铺图片列表Controller
 *
 * @author zhw
 * @date 2024-05-09
 */
@RestController
@RequestMapping("/shop/shopImages")
public class TShopImagesController extends BaseController {
    @Autowired
    private ITShopImagesService tShopImagesService;

    /**
     * 查询店铺图片列表列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:list')")
    @GetMapping("/list")
    public TableDataInfo list(TShopImages tShopImages) {
        startPage();
        List<TShopImages> list = tShopImagesService.selectTShopImagesList(tShopImages);
        return getDataTable(list);
    }

    /**
     * 导出店铺图片列表列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:export')")
    @Log(title = "店铺图片列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TShopImages tShopImages) {
        List<TShopImages> list = tShopImagesService.selectTShopImagesList(tShopImages);
        ExcelUtil<TShopImages> util = new ExcelUtil<TShopImages>(TShopImages.class);
        util.exportExcel(response, list, "店铺图片列表数据");
    }

    /**
     * 获取店铺图片列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tShopImagesService.selectTShopImagesById(id));
    }

    /**
     * 新增店铺图片列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:add')")
    @Log(title = "店铺图片列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TShopImages tShopImages) {
        tShopImages.setCreateBy(getUsername());
        tShopImages.setUserId(getUserId());
        tShopImages.setDeptId(getDeptId());
        return toAjax(tShopImagesService.insertTShopImages(tShopImages));
    }

    /**
     * 修改店铺图片列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:edit')")
    @Log(title = "店铺图片列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TShopImages tShopImages) {
        tShopImages.setUpdateBy(getUsername());
        return toAjax(tShopImagesService.updateTShopImages(tShopImages));
    }

    /**
     * 删除店铺图片列表
     */
    @PreAuthorize("@ss.hasPermi('shop:shopImages:remove')")
    @Log(title = "店铺图片列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tShopImagesService.deleteTShopImagesByIds(ids));
    }

    @GetMapping("/downloadShopImages")
    public void downloadShopImages(TShopImages tShopImages,HttpServletResponse response) throws Exception{
        List<String> resourceList = tShopImagesService.downloadShopImages(tShopImages);
        // 本地资源路径
        String localPath = RuoYiConfig.getProfile();
        List<File> fileList = resourceList.stream()
                .map(x->new File(localPath + StringUtils.substringAfter(x, Constants.RESOURCE_PREFIX))).collect(Collectors.toList());
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        String zipName = "prod_image.zip";
        FileUtils.setAttachmentResponseHeader(response, zipName);
        ZipUtil.zip(response.getOutputStream(), Charset.defaultCharset(),false,null, ArrayUtil.toArray(fileList,File.class));
    }
}
